package oauth2.config;

import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

import defineEx.MyAccessDeniedHandler;
import defineEx.MyAuthenticationEntryPoint;


/**
 * 
 * spring Security配置
 * @author monxz
 *
 */

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter{
	
	
	
	@Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        super.configure(resources);      
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
    	System.err.println("======加载Oauth2配置=====================");
                http.authorizeRequests()
                .antMatchers()
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()                   
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint((request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED))
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .httpBasic();

    }

}
